package com.ylzinfo.channelclient.controller;

import com.ylzinfo.channelclient.config.TyscConfig;
import com.ylzinfo.channelclient.exception.AppBizException;
import com.ylzinfo.channelclient.model.req.ObtainH5PageReq;
import com.ylzinfo.channelclient.model.req.ObtainH5TokenReq;
import com.ylzinfo.channelclient.model.resp.BaseH5Response;
import com.ylzinfo.channelclient.model.resp.ObtainH5ChannelTokenResp;
import com.ylzinfo.channelclient.model.resp.ObtainH5PageResp;
import com.ylzinfo.channelclient.remote.GgfwFeign;
import com.ylzinfo.channelclient.utils.JsonUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
@Controller
@RequiredArgsConstructor
public class H5Controller {

	private final TyscConfig tyscConfig;
	private final GgfwFeign ggfwFeign;
	private final HttpServletRequest httpServletRequest;

	@RequestMapping("/pullh5/{businessCode}/{terminalNo}/{aac002}/{aac003}")
	public String pullH5(@PathVariable("businessCode") String businessCode, @PathVariable("terminalNo") String terminalNo,
	                     @PathVariable("aac002") String aac002, @PathVariable("aac003") String aac003) {
		ObtainH5TokenReq req = ObtainH5TokenReq.builder()
				.userName(tyscConfig.getUserName())
				.passWord(tyscConfig.getUserPwd())
				.build();
		BaseH5Response<ObtainH5ChannelTokenResp> tokenResp = null;

		ggfwFeign.obtainToken(req).enqueue(new Callback<BaseH5Response<ObtainH5ChannelTokenResp>>() {
			@Override
			public void onResponse(Call<BaseH5Response<ObtainH5ChannelTokenResp>> call, Response<BaseH5Response<ObtainH5ChannelTokenResp>> response) {
				log.info("返回参数是:" + JsonUtils.toJson(response));
				if (!"1".equals(response.body().getStatus())) {
					throw new AppBizException(response.body().getRespCode(), response.body().getMessage());
				}
			}

			@Override
			public void onFailure(Call<BaseH5Response<ObtainH5ChannelTokenResp>> call, Throwable throwable) {
				log.error("请求发生异常");
				log.error(throwable.getMessage(), throwable);
				throw new AppBizException(throwable.getMessage());
			}
		});
		try {
			tokenResp = ggfwFeign.obtainToken(req).execute().body();
		} catch (IOException e) {
			e.printStackTrace();
		}
//        if (!"1".equals(tokenResp.getStatus())) {
//			throw new AppBizException(tokenResp.getRespCode(), tokenResp.getMessage());
//		}
		ObtainH5ChannelTokenResp data = tokenResp.getData();
		ObtainH5PageReq pageReq = ObtainH5PageReq.builder()
				.businessCode(businessCode)
				.channelToken(data.getChannelToken())
				.terminalNo(terminalNo)
				.ip("10.97.28.36")
				.mac("")
				.sn("212136069844")
				.aac002(aac002)
				.aac003(aac003)
				.build();
		Call<BaseH5Response<ObtainH5PageResp>> baseH5ResponseCall = ggfwFeign.obtainH5Page(pageReq);
		BaseH5Response<ObtainH5PageResp> pageResp = null;
		try {
			pageResp = baseH5ResponseCall.execute().body();
		} catch (IOException e) {
			throw new AppBizException(e.getMessage());
		}
		if (!"1".equals(pageResp.getStatus())) {
			throw new AppBizException(pageResp.getRespCode(), pageResp.getMessage());
		}
		ObtainH5PageResp page = pageResp.getData();
		String url = page.getUrl();
//        String url="http://127.0.0.1:9084/#/service-hall/participation-info?token=fbf00c08-bb0b-40f2-95c0-756939b44c66&accessToken=f4a6d58d-7f70-4cc6-851b-e954431a6bb6";
		return "redirect:" + url;
	}
}
